/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmMain.java
 *
 * Created on Oct 25, 2011, 1:44:45 PM
 */
package ttfsclient;

import static java.awt.SystemTray.getSystemTray;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.net.InetAddress;
import java.net.URL;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
import myutil.Utility;
import org.apache.commons.codec.digest.DigestUtils;
import pojo.POJODownload;
import pojo.POJODownloadClient;
import pojo.POJOFileClient;
import pojo.POJOFileUpload;
import pojo.POJOInfoClient;
import pojo.POJOInfoSharedFile;
import pojo.POJOMyInfo;
import pojo.POJOProcess;

/**
 *
 * @author Tidus Le
 */
public class frmMain extends javax.swing.JFrame 
{
    public static boolean changeInfo=true;
    public static boolean connected=false;
    public static boolean changeFileUpload=true;
    public static boolean changeSharedList=true;
    public static boolean changeDownload=true;
    
    public static POJOMyInfo myInfo=new POJOMyInfo();
    public static ArrayList<POJOFileUpload> arrFileUpload=new ArrayList<POJOFileUpload>();
    public static ArrayList<POJOInfoSharedFile> arrInfoSharedFiles=new ArrayList<POJOInfoSharedFile>();
    public static ArrayList<POJOFileClient> arrFileClient=new ArrayList<POJOFileClient>();
    public static ArrayList<POJOInfoClient> arrInfoClient=new ArrayList<POJOInfoClient>();
    ArrayList<POJOInfoSharedFile> arrFileList=new ArrayList<POJOInfoSharedFile>(); //Mảng để lưu thông tin của file được chia sẻ trong trường hợp tạm thời
    
    //variable for Download session
    public static ArrayList<POJODownload> arrDownload=new ArrayList<POJODownload>();
    public static ArrayList<POJODownloadClient> arrDownloadClient=new ArrayList<POJODownloadClient>();
    public static ArrayList<POJOProcess> arrProcess=new ArrayList<POJOProcess>();
    public static String tempDir=System.getProperty("java.io.tmpdir");
    public static final float maxData=500000;
    
    public static String fidPer="";
    long size=1;
    
    //variable for Open file
    String fPathOpen="";
    
    /** Creates new form frmMain */
    public frmMain() {
        initComponents();
                
        //Tray icon
        trayIcon();
                
        //Onload
        clsXMLMyInfo.readMyInfo(myInfo);
        clsXMLFileUpload.read(arrFileUpload);
        clsXMLDownload.read(arrDownload);
        clsXMLDownloadClient.read(arrDownloadClient);
        
        //Chuyển những file đang download nào chưa xong về chế độ chờ, để kết nối lại
        for(POJODownload down:arrDownload)
            if(down.getStatus().equals("download")==true)
                down.setStatus("quee");
        
        try
        {
            clsRequestSV.dinhDanh(myInfo,InetAddress.getLocalHost().getHostAddress());
        }catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
        lblID.setText(myInfo.getId());
        
        //start server threading
        clsThreadServer server=new clsThreadServer(Integer.parseInt(myInfo.getPort()));
        server.start();
        
        //Timer threading
        Timer timer=new Timer(100, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if(changeInfo==true)
                {
                    changeInfo=false;
                    loadMyInfo();
                    clsXMLMyInfo.writeMyInfo(myInfo);
                }
                
                if(connected==true)
                    lblStatus.setText("Connected");
                else
                    lblStatus.setText("Không thể kết nối đến server !");
                
                if(changeFileUpload==true)
                {
                    changeFileUpload=false;
                    clsXMLFileUpload.write(arrFileUpload);
                    loadFileUploadList();
                }
                
                if(changeSharedList==true)
                {
                    changeSharedList=false;
                    loadSharedFileList();
                }
                
                if(changeDownload==true)
                {
                    changeDownload=false;
                    clsXMLDownload.write(arrDownload);
                    clsXMLDownloadClient.write(arrDownloadClient);
                    loadDownloadFile();
                }
                
                //Kiem tra download
                createDownload();
                
                //tính số lượng phần trăm download
                if(fidPer.isEmpty()==false)
                {
                    if(fidPer.equals("full")==true)
                    {
                        probarComplete.setValue(100);
                        probarComplete.setString("Finish");
                        frmMain.this.setTitle("Client - Tata File Sharing");
                    }
                    else
                        if(fidPer.equals("")==false)
                        {
                            long currSize=Utility.sizeOfFolder(new File(tempDir+"\\"+fidPer));
                            long per=(currSize*100)/size;
                            probarComplete.setValue((int)per);
                            probarComplete.setString(per+"%");
                            frmMain.this.setTitle(per+"%"+" - Client - Tata File Sharing");
                        }
                        else
                        {
                            //Khi không có gì
                            probarComplete.setValue(0);
                            probarComplete.setString("");
                            frmMain.this.setTitle("Client - Tata File Sharing");                            
                        }
                }
                
                //update status Download List
                updateStatusDownloadList();
            }
        });
        timer.start();
        
        
        //Start timer for download
        //Timer threading
        Timer timerDownload=new Timer(1000, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                for(POJODownload down:arrDownload)
                    if(down.getStatus().equals("download")==true)
                    {
                        String fid=down.getFid();
                        boolean live=false;
                        
                        //Tìm xem còn thread download nao dang hoat dong hay khong
                        ThreadGroup thGroup=Thread.currentThread().getThreadGroup();
                        System.out.println(thGroup.activeCount());
                        Thread[] thr=new Thread[thGroup.activeCount()];
                        int n=thGroup.enumerate(thr, false);
                        for(int i=0;i<n;i++)
                        {
                            Thread t=thr[i];
                            if(t.getName().equals(fid)==true)
                            {
                                live=true;
                                break;
                            }
                        }
                        
                        //Nếu không có
                        if(live==false)
                        {
                            down.setStatus("quee");
                        }
                    }
            }
        });
        timerDownload.start();
    }
    
    /**
     * Thêm tray icon
     */
    private void trayIcon()
    {
        URL resource = getClass().getResource("/myutil/icon.png");
        Image image = Toolkit.getDefaultToolkit().getImage(resource);
        setIconImage(image);
        if (SystemTray.isSupported()) {
            final TrayIcon icon = new TrayIcon(image);
            icon.setToolTip("Tata File Sharing - Client");
            icon.setImageAutoSize(true);
            icon.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    frmMain.this.setVisible(true);
                    frmMain.this.setExtendedState(frmMain.NORMAL);
                    getSystemTray().remove(icon);
                }

            });
            addWindowListener(new WindowAdapter() {

                @Override
                public void windowIconified(WindowEvent e) {
                    frmMain.this.setVisible(false);
                    try {
                        getSystemTray().add(icon);
                    } catch (AWTException e1) {
                        e1.printStackTrace();
                    }
                }

            });
        }
    }
    
    /**
     * Hàm cập nhật status của danh sách các file đang download
     */
    private void updateStatusDownloadList()
    {
        for(int i=0;i<arrDownload.size();i++)
        {
            long currSize=Utility.sizeOfFolder(new File(tempDir+"\\"+arrDownload.get(i).getFid()));
            long per=(currSize*100)/arrDownload.get(i).getFsize();
            this.tblDownloadList.setValueAt(per+"%", i, 3);
            if(arrDownload.get(i).getStatus().equals("quee")==true)
                    this.tblDownloadList.setValueAt("Đang chờ download", i, 4);
                else
                    if(arrDownload.get(i).getStatus().equals("stop")==true)
                        this.tblDownloadList.setValueAt("Tạm dừng", i, 4);
                    else
                        if(arrDownload.get(i).getStatus().equals("download")==true)
                            this.tblDownloadList.setValueAt("Đang tiến hành download", i, 4);
                        else
                            if(arrDownload.get(i).getStatus().equals("finish")==true)
                            {
                                this.tblDownloadList.setValueAt("Đã hoàn tất", i, 4);
                                this.tblDownloadList.setValueAt("100%", i, 3);
                            }
        }
    }
    
    //////////////////////////////////////
    //Tao ra các threading download file//
    //////////////////////////////////////
    private void createDownload()
    {
        for(POJODownload down:arrDownload)
            if(down.getStatus().equals("quee")==true)
            {
                down.setStatus("download");
                int delay=1;
                for(POJODownloadClient dc:arrDownloadClient)
                    if(dc.getFid().equals(down.getFid())==true)
                    {
                        new clsThreadDownload(dc.getCid(), down, delay).start();
                        delay+=3;
                    }
                
                clsXMLDownload.write(arrDownload);
            }
    }
    
    /**
     * Load thông tin của máy tính
     */
    private void loadMyInfo()
    {
        this.lblID.setText(myInfo.getId());
        try
        {
            this.lblHostName.setText(InetAddress.getLocalHost().getHostName());
            this.lblMyIP.setText(InetAddress.getLocalHost().getHostAddress());
        }catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
    }
    
    /**
     * Load danh sách những file đang chia sẻ
     */
    private void loadFileUploadList()
    {
        DefaultTableModel dtm=new DefaultTableModel(){
            @Override
            public boolean isCellEditable(int row, int column)
            {
                return false;
            }
        };
        
        dtm.addColumn("File Name");
        dtm.addColumn("File ID");
        dtm.addColumn("Size");
        dtm.addColumn("Path");
        
        for(POJOFileUpload fu:arrFileUpload)
        {
            try
            {
                Vector vt=new Vector();
                File f=new File(fu.getPath());
                JLabel lbl=new JLabel(f.getName(), myutil.Utility.getFileIcon(f.getName()), SwingConstants.LEFT);
                vt.add(lbl);
                vt.add(fu.getFid());
                vt.add(Utility.getSize(f.length()));
                vt.add(fu.getPath());
                
                dtm.addRow(vt);
            }catch(Exception e)
            {
                System.out.println(e.getMessage());
            }
        }
        this.tblUploadList.setModel(dtm);
        this.tblUploadList.getColumnModel().getColumn(0).setCellRenderer(new myutil.myRender());
        
        //Setting width for column
        this.tblUploadList.getColumnModel().getColumn(0).setPreferredWidth(200);
        this.tblUploadList.getColumnModel().getColumn(1).setPreferredWidth(100);
        this.tblUploadList.getColumnModel().getColumn(2).setPreferredWidth(35);
        this.tblUploadList.getColumnModel().getColumn(3).setPreferredWidth(150);
        
        this.btnDeleteUpload.setEnabled(false);
    }
    
    /**
     * Load danh sách các file được chia sẻ, lấy từ server
     */
    private void loadSharedFileList()
    {
        DefaultTableModel dtm=new DefaultTableModel(){
            @Override
            public boolean isCellEditable(int row, int column)
            {
                return false;
            }
        };
        
        dtm.addColumn("File Name");
        dtm.addColumn("File ID");
        dtm.addColumn("Size");
        
        arrFileList=new ArrayList<POJOInfoSharedFile>();
        for(POJOInfoSharedFile fu:arrInfoSharedFiles)
        {
            try
            {
                Vector vt=new Vector();
                JLabel lbl=new JLabel(fu.getName(),myutil.Utility.getFileIcon(fu.getName()),SwingConstants.LEFT);
                vt.add(lbl);
                vt.add(fu.getFid());
                vt.add(Utility.getSize(fu.getSize()));
                
                arrFileList.add(fu);
                
                dtm.addRow(vt);
            }catch(Exception e)
            {
                System.out.println(e.getMessage());
            }
        }
        this.tblFileList.setModel(dtm);
        this.tblFileList.getColumnModel().getColumn(0).setCellRenderer(new myutil.myRender());
        
        //Setting width for column
        this.tblFileList.getColumnModel().getColumn(0).setPreferredWidth(200);
        this.tblFileList.getColumnModel().getColumn(1).setPreferredWidth(100);
        this.tblFileList.getColumnModel().getColumn(2).setPreferredWidth(35);
        
        //show clear client
        dtm=new DefaultTableModel(){
            @Override
            public boolean isCellEditable(int row, int column)
            {
                return false;
            }
        };

        dtm.addColumn("Tên Client");
        dtm.addColumn("Client ID");
        dtm.addColumn("Tình trạng");
        
        this.tblFileClient.setModel(dtm);
        this.btnDownload.setEnabled(false);
    }
    
    /**
     * Load danh sách các file được chia sẻ, lấy từ server
     */
    private void loadFileSearch(String key)
    {
        DefaultTableModel dtm=new DefaultTableModel(){
            @Override
            public boolean isCellEditable(int row, int column)
            {
                return false;
            }
        };
        
        dtm.addColumn("File Name");
        dtm.addColumn("File ID");
        dtm.addColumn("Size");
        
        arrFileList=new ArrayList<POJOInfoSharedFile>();
        
        for(POJOInfoSharedFile fu:arrInfoSharedFiles)
        {
            if(fu.getName().toLowerCase().contains(key.toLowerCase())==true)
            {
                try
                {
                    Vector vt=new Vector();
                    JLabel lbl=new JLabel(fu.getName(),myutil.Utility.getFileIcon(fu.getName()),SwingConstants.LEFT);
                    vt.add(lbl);
                    vt.add(fu.getFid());
                    vt.add(Utility.getSize(fu.getSize()));
                    
                    arrFileList.add(fu);

                    dtm.addRow(vt);
                }catch(Exception e)
                {
                    System.out.println(e.getMessage());
                }
            }
        }
        this.tblFileList.setModel(dtm);
        this.tblFileList.getColumnModel().getColumn(0).setCellRenderer(new myutil.myRender());
        
        //Setting width for column
        this.tblFileList.getColumnModel().getColumn(0).setPreferredWidth(200);
        this.tblFileList.getColumnModel().getColumn(1).setPreferredWidth(100);
        this.tblFileList.getColumnModel().getColumn(2).setPreferredWidth(35);
        
        //show clear client
        dtm=new DefaultTableModel(){
            @Override
            public boolean isCellEditable(int row, int column)
            {
                return false;
            }
        };

        dtm.addColumn("Tên Client");
        dtm.addColumn("Client ID");
        dtm.addColumn("Tình trạng");
        
        this.tblFileClient.setModel(dtm);
        this.btnDownload.setEnabled(false);
    }
    
    /**
     * Load danh sach những file đang download
     */
    private void loadDownloadFile()
    {
        DefaultTableModel dtm=new DefaultTableModel(){
            @Override
            public boolean isCellEditable(int row, int column)
            {
                return false;
            }
        };
        
        dtm.addColumn("File Name");
        dtm.addColumn("Size");
        dtm.addColumn("Path Save");
        dtm.addColumn("Complete");
        dtm.addColumn("Status");
        
        for(POJODownload fu:arrDownload)
        {
            try
            {
                Vector vt=new Vector();
                JLabel lbl=new JLabel(fu.getFname(),myutil.Utility.getFileIcon(fu.getFname()),SwingConstants.LEFT);
                vt.add(lbl);
                vt.add(Utility.getSize(fu.getFsize()));
                vt.add(fu.getFpath());
                vt.add("0 %");
                if(fu.getStatus().equals("quee")==true)
                    vt.add("Đang chờ download");
                else
                    if(fu.getStatus().equals("stop")==true)
                        vt.add("Tạm dừng");
                    else
                        if(fu.getStatus().equals("download")==true)
                            vt.add("Đang tiến hành download");
                        else
                            if(fu.getStatus().equals("finish")==true)
                                vt.add("Đã hoàn tất");
                
                dtm.addRow(vt);
            }catch(Exception e)
            {
                System.out.println(e.getMessage());
            }
        }
        
        this.tblDownloadList.setModel(dtm);
        this.tblDownloadList.getColumnModel().getColumn(0).setCellRenderer(new myutil.myRender());
        
        //Setting width for column
        this.tblDownloadList.getColumnModel().getColumn(0).setPreferredWidth(200);
        this.tblDownloadList.getColumnModel().getColumn(1).setPreferredWidth(35);
        this.tblDownloadList.getColumnModel().getColumn(2).setPreferredWidth(100);
        this.tblDownloadList.getColumnModel().getColumn(3).setPreferredWidth(33);
        
        //set cho download client
        dtm=new DefaultTableModel(){
            @Override
            public boolean isCellEditable(int row, int column)
            {
                return false;
            }
        };
        
        dtm.addColumn("Client Name");
        dtm.addColumn("Client ID");
        this.tblDownloadClient.setModel(dtm);
        
        //set enable cho button
        btnStop.setEnabled(false);
        btnResume.setEnabled(false);
        btnDeleteDownload.setEnabled(false);
    }
    
    /**
     * load ra danh sach client ma file nay download
     * @param fid 
     */
    private void loadDownloadClient(String fid)
    {
        //set cho download client
        DefaultTableModel dtm=new DefaultTableModel(){
            @Override
            public boolean isCellEditable(int row, int column)
            {
                return false;
            }
        };
        
        dtm.addColumn("Client Name");
        dtm.addColumn("Client ID");
        
        for(POJODownloadClient fu:arrDownloadClient)
        {
            if(fu.getFid().equals(fid)==true)
            {
                try
                {
                    Vector vt=new Vector();
                    vt.add(fu.getCname());
                    vt.add(fu.getCid());

                    dtm.addRow(vt);
                }catch(Exception e)
                {
                    System.out.println(e.getMessage());
                }
            }
        }
        this.tblDownloadClient.setModel(dtm);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fileChooser = new javax.swing.JFileChooser();
        listFileClientID = new javax.swing.JList();
        pmnuFile = new javax.swing.JPopupMenu();
        mnuOpen = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        mnuOpenFolder = new javax.swing.JMenuItem();
        jToolBar3 = new javax.swing.JToolBar();
        jLabel1 = new javax.swing.JLabel();
        lblID = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        lblHostName = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        lblMyIP = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        lblStatus = new javax.swing.JLabel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jToolBar1 = new javax.swing.JToolBar();
        btnResume = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        btnStop = new javax.swing.JButton();
        jSeparator3 = new javax.swing.JToolBar.Separator();
        btnDeleteDownload = new javax.swing.JButton();
        jSeparator7 = new javax.swing.JToolBar.Separator();
        jSeparator8 = new javax.swing.JToolBar.Separator();
        jSeparator9 = new javax.swing.JToolBar.Separator();
        btnResumeAll = new javax.swing.JButton();
        jSeparator10 = new javax.swing.JToolBar.Separator();
        btnStopAll = new javax.swing.JButton();
        jSplitPane2 = new javax.swing.JSplitPane();
        jScrollPane5 = new javax.swing.JScrollPane();
        tblDownloadList = new javax.swing.JTable();
        jScrollPane6 = new javax.swing.JScrollPane();
        tblDownloadClient = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jToolBar4 = new javax.swing.JToolBar();
        btnShareFile = new javax.swing.JButton();
        jSeparator6 = new javax.swing.JToolBar.Separator();
        btnDeleteUpload = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblUploadList = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        jToolBar5 = new javax.swing.JToolBar();
        jButton1 = new javax.swing.JButton();
        jSeparator5 = new javax.swing.JToolBar.Separator();
        btnDownload = new javax.swing.JButton();
        jSeparator4 = new javax.swing.JToolBar.Separator();
        txtSearch = new javax.swing.JTextField();
        btnSearchCancel = new javax.swing.JButton();
        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane3 = new javax.swing.JScrollPane();
        tblFileList = new javax.swing.JTable();
        jScrollPane4 = new javax.swing.JScrollPane();
        tblFileClient = new javax.swing.JTable();
        probarComplete = new javax.swing.JProgressBar();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        mnuExit = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        mnuConfig = new javax.swing.JMenuItem();
        mnuHelp = new javax.swing.JMenu();
        mnuAbout = new javax.swing.JMenuItem();

        fileChooser.setDialogTitle("Open file which you want to share...");

        listFileClientID.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

        mnuOpen.setText("Open");
        mnuOpen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuOpenActionPerformed(evt);
            }
        });
        pmnuFile.add(mnuOpen);
        pmnuFile.add(jSeparator1);

        mnuOpenFolder.setText("Open Folder");
        mnuOpenFolder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuOpenFolderActionPerformed(evt);
            }
        });
        pmnuFile.add(mnuOpenFolder);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Client - Tata File Sharing");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jToolBar3.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jToolBar3.setFloatable(false);
        jToolBar3.setRollover(true);

        jLabel1.setText("My ID: ");
        jToolBar3.add(jLabel1);

        lblID.setForeground(new java.awt.Color(102, 0, 204));
        lblID.setText("Undefined ");
        jToolBar3.add(lblID);

        jLabel2.setText(" | ");
        jToolBar3.add(jLabel2);

        jLabel3.setText(" Computer Name: ");
        jToolBar3.add(jLabel3);

        lblHostName.setForeground(new java.awt.Color(102, 0, 204));
        lblHostName.setText("Undefined ");
        jToolBar3.add(lblHostName);

        jLabel4.setText(" | ");
        jToolBar3.add(jLabel4);

        jLabel5.setText(" My IP: ");
        jToolBar3.add(jLabel5);

        lblMyIP.setForeground(new java.awt.Color(102, 0, 204));
        lblMyIP.setText("Undefined ");
        jToolBar3.add(lblMyIP);

        jLabel6.setText(" | ");
        jToolBar3.add(jLabel6);

        jLabel7.setText(" Status: ");
        jToolBar3.add(jLabel7);

        lblStatus.setForeground(new java.awt.Color(102, 0, 204));
        lblStatus.setText("Không thể kết nối đến server!");
        jToolBar3.add(lblStatus);

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        btnResume.setIcon(new javax.swing.ImageIcon(getClass().getResource("/myutil/icon/resume.png"))); // NOI18N
        btnResume.setText("Resume");
        btnResume.setToolTipText("Resume download");
        btnResume.setEnabled(false);
        btnResume.setFocusable(false);
        btnResume.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnResume.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnResume.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResumeActionPerformed(evt);
            }
        });
        jToolBar1.add(btnResume);
        jToolBar1.add(jSeparator2);

        btnStop.setIcon(new javax.swing.ImageIcon(getClass().getResource("/myutil/icon/stop.png"))); // NOI18N
        btnStop.setText("Stop");
        btnStop.setToolTipText("Stop download");
        btnStop.setEnabled(false);
        btnStop.setFocusable(false);
        btnStop.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnStop.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStopActionPerformed(evt);
            }
        });
        jToolBar1.add(btnStop);
        jToolBar1.add(jSeparator3);

        btnDeleteDownload.setIcon(new javax.swing.ImageIcon(getClass().getResource("/myutil/icon/delete 1.png"))); // NOI18N
        btnDeleteDownload.setText("Delete");
        btnDeleteDownload.setToolTipText("Delete file đang chọn");
        btnDeleteDownload.setEnabled(false);
        btnDeleteDownload.setFocusable(false);
        btnDeleteDownload.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnDeleteDownload.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/myutil/icon/delete 2.png"))); // NOI18N
        btnDeleteDownload.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnDeleteDownload.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteDownloadActionPerformed(evt);
            }
        });
        jToolBar1.add(btnDeleteDownload);
        jToolBar1.add(jSeparator7);
        jToolBar1.add(jSeparator8);
        jToolBar1.add(jSeparator9);

        btnResumeAll.setIcon(new javax.swing.ImageIcon(getClass().getResource("/myutil/icon/resumeAll.png"))); // NOI18N
        btnResumeAll.setText("Resume All");
        btnResumeAll.setToolTipText("Resume tất cả các file đang tạm dừng");
        btnResumeAll.setFocusable(false);
        btnResumeAll.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnResumeAll.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnResumeAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResumeAllActionPerformed(evt);
            }
        });
        jToolBar1.add(btnResumeAll);
        jToolBar1.add(jSeparator10);

        btnStopAll.setIcon(new javax.swing.ImageIcon(getClass().getResource("/myutil/icon/stopAll.png"))); // NOI18N
        btnStopAll.setText("Stop All");
        btnStopAll.setToolTipText("Stop tất cả các file đang download");
        btnStopAll.setFocusable(false);
        btnStopAll.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnStopAll.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnStopAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStopAllActionPerformed(evt);
            }
        });
        jToolBar1.add(btnStopAll);

        jSplitPane2.setDividerLocation(230);
        jSplitPane2.setDividerSize(3);
        jSplitPane2.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        tblDownloadList.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblDownloadList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tblDownloadList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                tblDownloadListMouseReleased(evt);
            }
        });
        jScrollPane5.setViewportView(tblDownloadList);

        jSplitPane2.setTopComponent(jScrollPane5);

        tblDownloadClient.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblDownloadClient.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane6.setViewportView(tblDownloadClient);

        jSplitPane2.setRightComponent(jScrollPane6);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 590, Short.MAX_VALUE)
            .addComponent(jSplitPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 590, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSplitPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 322, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Download List", new javax.swing.ImageIcon(getClass().getResource("/myutil/icon/downloadList.png")), jPanel1, "Danh sách các file download từ cộng đồng"); // NOI18N

        jToolBar4.setFloatable(false);
        jToolBar4.setRollover(true);

        btnShareFile.setIcon(new javax.swing.ImageIcon(getClass().getResource("/myutil/icon/upload.png"))); // NOI18N
        btnShareFile.setText("Share File");
        btnShareFile.setToolTipText("Chọn file để share lên server");
        btnShareFile.setFocusable(false);
        btnShareFile.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnShareFile.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnShareFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnShareFileActionPerformed(evt);
            }
        });
        jToolBar4.add(btnShareFile);
        jToolBar4.add(jSeparator6);

        btnDeleteUpload.setIcon(new javax.swing.ImageIcon(getClass().getResource("/myutil/icon/delete 1.png"))); // NOI18N
        btnDeleteUpload.setText("Delete");
        btnDeleteUpload.setToolTipText("Delete file đang chọn");
        btnDeleteUpload.setEnabled(false);
        btnDeleteUpload.setFocusable(false);
        btnDeleteUpload.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnDeleteUpload.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/myutil/icon/delete 2.png"))); // NOI18N
        btnDeleteUpload.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnDeleteUpload.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteUploadActionPerformed(evt);
            }
        });
        jToolBar4.add(btnDeleteUpload);

        tblUploadList.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblUploadList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tblUploadList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                tblUploadListMouseReleased(evt);
            }
        });
        jScrollPane2.setViewportView(tblUploadList);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar4, javax.swing.GroupLayout.DEFAULT_SIZE, 590, Short.MAX_VALUE)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 590, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jToolBar4, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 322, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Upload List", new javax.swing.ImageIcon(getClass().getResource("/myutil/icon/uploadList.png")), jPanel2, "Danh sách các file chia sẻ vào cộng đồng"); // NOI18N

        jToolBar5.setFloatable(false);
        jToolBar5.setRollover(true);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/myutil/icon/getlist.png"))); // NOI18N
        jButton1.setText("Lấy danh sách");
        jButton1.setToolTipText("Lấy toàn bộ dan sách từ server");
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jToolBar5.add(jButton1);
        jToolBar5.add(jSeparator5);

        btnDownload.setIcon(new javax.swing.ImageIcon(getClass().getResource("/myutil/icon/download.png"))); // NOI18N
        btnDownload.setText("Download");
        btnDownload.setToolTipText("Download file đang chọn");
        btnDownload.setEnabled(false);
        btnDownload.setFocusable(false);
        btnDownload.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnDownload.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnDownload.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDownloadActionPerformed(evt);
            }
        });
        jToolBar5.add(btnDownload);
        jToolBar5.add(jSeparator4);

        txtSearch.setBackground(new java.awt.Color(255, 255, 204));
        txtSearch.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtSearch.setText("Search here");
        txtSearch.setToolTipText("Nhập tên file cần tìm kiếm");
        txtSearch.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtSearchFocusGained(evt);
            }
        });
        txtSearch.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtSearchKeyReleased(evt);
            }
        });
        jToolBar5.add(txtSearch);

        btnSearchCancel.setText("x");
        btnSearchCancel.setToolTipText("Hủy search");
        btnSearchCancel.setFocusable(false);
        btnSearchCancel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnSearchCancel.setMaximumSize(new java.awt.Dimension(19, 90));
        btnSearchCancel.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnSearchCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchCancelActionPerformed(evt);
            }
        });
        jToolBar5.add(btnSearchCancel);

        jSplitPane1.setDividerLocation(230);
        jSplitPane1.setDividerSize(3);
        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        tblFileList.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblFileList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tblFileList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                tblFileListMouseReleased(evt);
            }
        });
        jScrollPane3.setViewportView(tblFileList);

        jSplitPane1.setTopComponent(jScrollPane3);

        tblFileClient.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblFileClient.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                tblFileClientMouseReleased(evt);
            }
        });
        jScrollPane4.setViewportView(tblFileClient);

        jSplitPane1.setRightComponent(jScrollPane4);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar5, javax.swing.GroupLayout.DEFAULT_SIZE, 590, Short.MAX_VALUE)
            .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 590, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jToolBar5, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 322, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Danh Sách File", new javax.swing.ImageIcon(getClass().getResource("/myutil/icon/fileList.png")), jPanel3, "Danh sách các file cộng đồng chia sẻ lên server"); // NOI18N

        probarComplete.setForeground(new java.awt.Color(159, 67, 251));
        probarComplete.setStringPainted(true);

        jMenu1.setText("File");

        mnuExit.setText("Exit");
        mnuExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuExitActionPerformed(evt);
            }
        });
        jMenu1.add(mnuExit);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");

        mnuConfig.setText("Configuration");
        mnuConfig.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuConfigActionPerformed(evt);
            }
        });
        jMenu2.add(mnuConfig);

        jMenuBar1.add(jMenu2);

        mnuHelp.setText("Help");

        mnuAbout.setText("About");
        mnuAbout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuAboutActionPerformed(evt);
            }
        });
        mnuHelp.add(mnuAbout);

        jMenuBar1.add(mnuHelp);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar3, javax.swing.GroupLayout.DEFAULT_SIZE, 595, Short.MAX_VALUE)
            .addComponent(probarComplete, javax.swing.GroupLayout.DEFAULT_SIZE, 595, Short.MAX_VALUE)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 595, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 459, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(probarComplete, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jToolBar3, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        clsRequestSV.offline(myInfo);
    }//GEN-LAST:event_formWindowClosing

    private void mnuExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuExitActionPerformed
        clsRequestSV.offline(myInfo);
        System.exit(0);
    }//GEN-LAST:event_mnuExitActionPerformed

    private void mnuConfigActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuConfigActionPerformed
        frmConfigDlg config=new frmConfigDlg(this, true);
        config.setVisible(true);
    }//GEN-LAST:event_mnuConfigActionPerformed

    private void mnuAboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuAboutActionPerformed
        frmAbout about=new frmAbout(this, true);
        about.setVisible(true);
    }//GEN-LAST:event_mnuAboutActionPerformed

    private void mnuOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuOpenActionPerformed
        (new clsThreadOpenFile(fPathOpen)).start();
    }//GEN-LAST:event_mnuOpenActionPerformed

    private void mnuOpenFolderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuOpenFolderActionPerformed
        (new clsThreadOpenFile(new File(fPathOpen).getParent())).start();
    }//GEN-LAST:event_mnuOpenFolderActionPerformed

    private void btnShareFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnShareFileActionPerformed
        try
        {
            int result=fileChooser.showOpenDialog(this);
            if(result==JFileChooser.APPROVE_OPTION)
            {
                File f=fileChooser.getSelectedFile();
                FileInputStream fInStr=new FileInputStream(f);
                String fid=DigestUtils.md5Hex(fInStr);

                clsRequestSV.shareFile(fid, f.getName(), f.length(),myInfo);

                //lưu thông tin lại
                boolean exist=false;
                for(POJOFileUpload fuload:arrFileUpload)
                    if(fuload.getFid().equals(fid)==true)
                    {
                        exist=true;
                        break;
                    }

                if(exist==false)
                {
                    POJOFileUpload fu=new POJOFileUpload();
                    fu.setFid(fid);
                    fu.setPath(f.getPath());
                    frmMain.changeFileUpload=true;
                    frmMain.arrFileUpload.add(fu);
                }
            }
        }catch(Exception e)
        {
            JOptionPane.showMessageDialog(new JFrame(), e.getMessage(),"Lỗi !",JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnShareFileActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        clsRequestSV.getLlistSharedFile(myInfo);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void btnSearchCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchCancelActionPerformed
        txtSearch.setText("");
        loadSharedFileList();
        tblFileList.requestFocus();
    }//GEN-LAST:event_btnSearchCancelActionPerformed

    private void txtSearchFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtSearchFocusGained
        txtSearch.selectAll();
}//GEN-LAST:event_txtSearchFocusGained

    private void txtSearchKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtSearchKeyReleased
        if(evt.getKeyCode()==KeyEvent.VK_ESCAPE)
        {
            txtSearch.setText("");
            loadSharedFileList();
            tblFileList.requestFocus();
        }
        else
            loadFileSearch(txtSearch.getText());
    }//GEN-LAST:event_txtSearchKeyReleased

    private void btnDownloadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDownloadActionPerformed
        if (this.tblFileClient.getSelectedRowCount()>=1)
        {
            if (this.tblFileList.getSelectedRowCount()>=1)
            {
                String fileName=arrFileList.get(this.tblFileList.getSelectedRow()).getName();
                String fid=arrFileList.get(this.tblFileList.getSelectedRow()).getFid();
                //Kiem tra ton tai hay chua
                for(POJODownload down:arrDownload)
                    if(down.getFid().equals(fid)==true)
                    {
                        JOptionPane.showMessageDialog(this, "File này đã download rồi! Bạn hãy chọn file khác.","Thông báo",JOptionPane.WARNING_MESSAGE);
                        return;
                    }

                fileChooser.setSelectedFile(new File(fileName));
                int result=fileChooser.showSaveDialog(this);
                if(result==JFileChooser.APPROVE_OPTION)
                {
                    String path=fileChooser.getSelectedFile().getPath();

                    POJODownload down=new POJODownload();
                    down.setFid(fid);
                    down.setFname(fileName);
                    down.setFpath(path);
                    down.setFsize(arrFileList.get(this.tblFileList.getSelectedRow()).getSize());
                    down.setStatus("quee");
                    arrDownload.add(down); //add

                    //add vô danh sách chia sẻ file trên server
                    clsRequestSV.shareFile(fid, fileName, arrFileList.get(this.tblFileList.getSelectedRow()).getSize(), myInfo);

                    POJODownloadClient downClient;
                    int[] cid=this.tblFileClient.getSelectedRows();

                    for(int i:cid)
                    {
                        downClient=new POJODownloadClient();
                        downClient.setFid(fid);
                        String clientid=(String)listFileClientID.getModel().getElementAt(i);
                        downClient.setCid(clientid);
                        for(POJOInfoClient ic:arrInfoClient)
                            if(ic.getId().equals(clientid)==true)
                                downClient.setCname(ic.getName());

                        //Tim xem đã có trong mảng danh sách hay chưa
                        boolean exist=false;
                        for(POJODownloadClient dc:arrDownloadClient)
                            if((dc.getFid().equals(downClient.getFid())==true)&&(dc.getCid().equals(downClient.getCid())==true))
                            {
                                exist=true;
                                break;
                            }

                        if(exist==false)
                            arrDownloadClient.add(downClient); //add
                    }

                    //notify to change data
                    fidPer=fid;
                    size=arrFileList.get(this.tblFileList.getSelectedRow()).getSize();
                    frmMain.changeDownload=true;
                    this.jTabbedPane1.setSelectedIndex(0);
                }
            }
        }
        else
        {
            if (this.tblFileList.getSelectedRowCount()>=1)
            {
                String fileName=arrFileList.get(this.tblFileList.getSelectedRow()).getName();
                String fid=arrFileList.get(this.tblFileList.getSelectedRow()).getFid();
                //Kiem tra ton tai hay chua
                for(POJODownload down:arrDownload)
                    if(down.getFid().equals(fid)==true)
                    {
                        JOptionPane.showMessageDialog(this, "File này đã download rồi! Bạn hãy chọn file khác.","Thông báo",JOptionPane.WARNING_MESSAGE);
                        return;
                    }

                fileChooser.setSelectedFile(new File(fileName));
                int result=fileChooser.showSaveDialog(this);
                if(result==JFileChooser.APPROVE_OPTION)
                {
                    String path=fileChooser.getSelectedFile().getPath();

                    POJODownload down=new POJODownload();
                    down.setFid(fid);
                    down.setFname(fileName);
                    down.setFpath(path);
                    down.setFsize(arrFileList.get(this.tblFileList.getSelectedRow()).getSize());
                    down.setStatus("quee");
                    arrDownload.add(down); //add

                    //add vô danh sách chia sẻ file trên server
                    clsRequestSV.shareFile(fid, fileName, arrFileList.get(this.tblFileList.getSelectedRow()).getSize(), myInfo);

                    POJODownloadClient downClient;
                    int cidCount=this.tblFileClient.getRowCount();

                    for(int i=0;i<cidCount;i++)
                    {
                        downClient=new POJODownloadClient();
                        downClient.setFid(fid);
                        String clientid=(String)listFileClientID.getModel().getElementAt(i);
                        downClient.setCid(clientid);
                        for(POJOInfoClient ic:arrInfoClient)
                            if(ic.getId().equals(clientid)==true)
                                downClient.setCname(ic.getName());

                        //Tim xem đã có trong mảng danh sách hay chưa
                        boolean exist=false;
                        for(POJODownloadClient dc:arrDownloadClient)
                            if((dc.getFid().equals(downClient.getFid())==true)&&(dc.getCid().equals(downClient.getCid())==true))
                            {
                                exist=true;
                                break;
                            }

                        if(exist==false)
                            arrDownloadClient.add(downClient); //add
                    }

                    //notify to change data
                    fidPer=fid;
                    size=arrFileList.get(this.tblFileList.getSelectedRow()).getSize();
                    frmMain.changeDownload=true;
                    this.jTabbedPane1.setSelectedIndex(0);
                }
            }
        }
    }//GEN-LAST:event_btnDownloadActionPerformed

    private void btnResumeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResumeActionPerformed
        if(this.tblDownloadList.getSelectedRowCount()>=1)
        {
            arrDownload.get(this.tblDownloadList.getSelectedRow()).setStatus("quee");
            //this.tblDownloadList.setValueAt("đang chờ", this.tblDownloadList.getSelectedRow(), 4);
            clsXMLDownload.write(arrDownload);
            
            //set enable của button
            btnStop.setEnabled(true);
            btnResume.setEnabled(false);
            btnDeleteDownload.setEnabled(false);
            //frmMain.changeDownload=true;
        }
    }//GEN-LAST:event_btnResumeActionPerformed

    private void btnStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStopActionPerformed
        if(this.tblDownloadList.getSelectedRowCount()>=1)
        {
            arrDownload.get(this.tblDownloadList.getSelectedRow()).setStatus("stop");
            //this.tblDownloadList.setValueAt("đã dừng", this.tblDownloadList.getSelectedRow(), 4);
            clsXMLDownload.write(arrDownload);
            
            //set enable của button
            btnStop.setEnabled(false);
            btnResume.setEnabled(true);
            btnDeleteDownload.setEnabled(true);
            //frmMain.changeDownload=true;
        }
    }//GEN-LAST:event_btnStopActionPerformed

    private void btnDeleteDownloadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteDownloadActionPerformed
        if(this.tblDownloadList.getSelectedRowCount()>=1)
        {
            this.fidPer="";
            
            String fid=arrDownload.get(this.tblDownloadList.getSelectedRow()).getFid();
            for(POJODownload down:arrDownload)
                if(down.getFid().equals(fid)==true)
                {
                    arrDownload.remove(down);
                    break;
                }
            
            //xóa thong tin trong mang Download Client
            int c=0;
            while(c<arrDownloadClient.size())
            {
                if(arrDownloadClient.get(c).getFid().equals(fid)==true)
                    arrDownloadClient.remove(c);
                else
                    c++;
            }
            
            frmMain.changeDownload=true;
        }
    }//GEN-LAST:event_btnDeleteDownloadActionPerformed

    private void btnDeleteUploadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteUploadActionPerformed
        if(this.tblUploadList.getSelectedRowCount()>=1)
        {
            String fid=arrFileUpload.get(this.tblUploadList.getSelectedRow()).getFid();
            clsRequestSV.deleteSharedFile(myInfo, fid);
            
            arrFileUpload.remove(this.tblUploadList.getSelectedRow());
            //notify to frmMain
            frmMain.changeFileUpload=true;
        }
    }//GEN-LAST:event_btnDeleteUploadActionPerformed

    private void btnResumeAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResumeAllActionPerformed
        for(POJODownload down:arrDownload)
        {
            if(down.getStatus().equals("stop")==true)
            {
                down.setStatus("quee");
                clsXMLDownload.write(arrDownload);
            }
        }
    }//GEN-LAST:event_btnResumeAllActionPerformed

    private void btnStopAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStopAllActionPerformed
        for(POJODownload down:arrDownload)
        {
            if(down.getStatus().equals("finish")==false)
            {
                down.setStatus("stop");
                clsXMLDownload.write(arrDownload);
            }
        }
    }//GEN-LAST:event_btnStopAllActionPerformed

    private void tblFileListMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblFileListMouseReleased
        if (this.tblFileList.getSelectedRowCount()>=1)
        {
            //set enable for download button
            btnDownload.setEnabled(true);
            
            //show information
            String fid=arrFileList.get(tblFileList.getSelectedRow()).getFid();
            
            //show client
            DefaultTableModel dtm=new DefaultTableModel(){
                @Override
                public boolean isCellEditable(int row, int column)
                {
                    return false;
                }
            };

            dtm.addColumn("Tên Client");
            dtm.addColumn("Client ID");
            dtm.addColumn("Tình trạng");
            
            DefaultListModel model_cid=new DefaultListModel();
            for(POJOFileClient fc:arrFileClient)
                if(fc.getFid().equals(fid)==true)
                {
                    for(POJOInfoClient ic:arrInfoClient)
                    {
                        if(ic.getId().equals(fc.getCid())==true)
                        {
                            try
                            {
                                model_cid.addElement(ic.getId());
                                Vector vt=new Vector();
                                if(ic.getStatus().equals("on")==true)
                                {
                                    vt.add(ic.getName());
                                    vt.add(ic.getId());
                                    vt.add("Online");
                                }
                                else
                                {
                                    vt.add(ic.getName());
                                    vt.add(ic.getId());
                                    vt.add("Offline");
                                }
                                
                                dtm.addRow(vt);
                            }catch(Exception e)
                            {
                                System.out.println(e.getMessage());
                            }
                        }
                    }
                }
            listFileClientID.setModel(model_cid);
            this.tblFileClient.setModel(dtm);
        }
        else
            btnDownload.setEnabled(false);
    }//GEN-LAST:event_tblFileListMouseReleased

    private void tblFileClientMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblFileClientMouseReleased
        if (this.tblFileList.getSelectedRowCount()>=1)
        {
            btnDownload.setEnabled(true);
        }
        else
            btnDownload.setEnabled(false);
    }//GEN-LAST:event_tblFileClientMouseReleased

    private void tblUploadListMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblUploadListMouseReleased
        if(this.tblUploadList.getSelectedRowCount()>=1)
        {
            this.btnDeleteUpload.setEnabled(true);
            
            //case to show popup menu
            if(evt.getClickCount()==2)
            {
                (new clsThreadOpenFile(arrFileUpload.get(this.tblUploadList.getSelectedRow()).getPath())).start();
            }
            else
                if(evt.getButton()==MouseEvent.BUTTON3)
                {
                    fPathOpen=arrFileUpload.get(this.tblUploadList.getSelectedRow()).getPath();
                    pmnuFile.show(evt.getComponent(), evt.getX(), evt.getY());
                }
        }
        else
            this.btnDeleteUpload.setEnabled(false);
    }//GEN-LAST:event_tblUploadListMouseReleased

    private void tblDownloadListMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblDownloadListMouseReleased
        if (this.tblDownloadList.getSelectedRowCount()>=1)
        {
            if(arrDownload.get(this.tblDownloadList.getSelectedRow()).getStatus().equals("finish")==true)
                fidPer="full";
            else
                fidPer=arrDownload.get(this.tblDownloadList.getSelectedRow()).getFid();
            size=arrDownload.get(this.tblDownloadList.getSelectedRow()).getFsize();
            loadDownloadClient(arrDownload.get(this.tblDownloadList.getSelectedRow()).getFid());
            
            if(arrDownload.get(this.tblDownloadList.getSelectedRow()).getStatus().equals("download")==true)
            {
                btnStop.setEnabled(true);
                btnResume.setEnabled(false);
                btnDeleteDownload.setEnabled(false);
            }
            else
                if(arrDownload.get(this.tblDownloadList.getSelectedRow()).getStatus().equals("stop")==true)
                {
                    btnStop.setEnabled(false);
                    btnResume.setEnabled(true);
                    btnDeleteDownload.setEnabled(true);
                }
                else
                    if(arrDownload.get(this.tblDownloadList.getSelectedRow()).getStatus().equals("finish")==true)
                    {
                        btnStop.setEnabled(false);
                        btnResume.setEnabled(false);
                        btnDeleteDownload.setEnabled(true);
                        
                        //case to show popup menu
                        if(evt.getClickCount()==2)
                        {
                            (new clsThreadOpenFile(arrDownload.get(this.tblDownloadList.getSelectedRow()).getFpath())).start();
                        }
                        else
                            if(evt.getButton()==MouseEvent.BUTTON3)
                            {
                                fPathOpen=arrDownload.get(this.tblDownloadList.getSelectedRow()).getFpath();
                                pmnuFile.show(evt.getComponent(), evt.getX(), evt.getY());
                            }
                    }
                    else
                    {
                        btnStop.setEnabled(false);
                        btnResume.setEnabled(false);
                        btnDeleteDownload.setEnabled(false);
                    }
            
        }
        else
        {
            btnDeleteDownload.setEnabled(false);
            btnStop.setEnabled(false);
            btnResume.setEnabled(false);
        }
    }//GEN-LAST:event_tblDownloadListMouseReleased

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try
                {
                    UIManager.setLookAndFeel("com.jtattoo.plaf.mcwin.McWinLookAndFeel");
                }catch(Exception e)
                {
                    e.printStackTrace();
                }
                new frmMain().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDeleteDownload;
    private javax.swing.JButton btnDeleteUpload;
    private javax.swing.JButton btnDownload;
    private javax.swing.JButton btnResume;
    private javax.swing.JButton btnResumeAll;
    private javax.swing.JButton btnSearchCancel;
    private javax.swing.JButton btnShareFile;
    private javax.swing.JButton btnStop;
    private javax.swing.JButton btnStopAll;
    private javax.swing.JFileChooser fileChooser;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator10;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JToolBar.Separator jSeparator3;
    private javax.swing.JToolBar.Separator jSeparator4;
    private javax.swing.JToolBar.Separator jSeparator5;
    private javax.swing.JToolBar.Separator jSeparator6;
    private javax.swing.JToolBar.Separator jSeparator7;
    private javax.swing.JToolBar.Separator jSeparator8;
    private javax.swing.JToolBar.Separator jSeparator9;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JSplitPane jSplitPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JToolBar jToolBar3;
    private javax.swing.JToolBar jToolBar4;
    private javax.swing.JToolBar jToolBar5;
    private javax.swing.JLabel lblHostName;
    private javax.swing.JLabel lblID;
    private javax.swing.JLabel lblMyIP;
    private javax.swing.JLabel lblStatus;
    private javax.swing.JList listFileClientID;
    private javax.swing.JMenuItem mnuAbout;
    private javax.swing.JMenuItem mnuConfig;
    private javax.swing.JMenuItem mnuExit;
    private javax.swing.JMenu mnuHelp;
    private javax.swing.JMenuItem mnuOpen;
    private javax.swing.JMenuItem mnuOpenFolder;
    private javax.swing.JPopupMenu pmnuFile;
    private javax.swing.JProgressBar probarComplete;
    private javax.swing.JTable tblDownloadClient;
    private javax.swing.JTable tblDownloadList;
    private javax.swing.JTable tblFileClient;
    private javax.swing.JTable tblFileList;
    private javax.swing.JTable tblUploadList;
    private javax.swing.JTextField txtSearch;
    // End of variables declaration//GEN-END:variables
}
